package com.ptszyxx.popose.module.main.home.adapter;

import android.view.View;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.viewholder.BaseDataBindingHolder;
import com.ptszyxx.popose.R;
import com.ptszyxx.popose.common.enums.RankEnum;
import com.ptszyxx.popose.databinding.ItemHomeRankBinding;
import com.ptszyxx.popose.module.main.home.vm.HomeRankDataVM;
import com.ptszyxx.popose.module.main.home.vm.HomeVM;
import com.ysg.http.data.entity.home.HomeRankEntity;
import com.ysg.http.data.entity.home.HomeResult;
import com.ysg.utils.YImageUtil;
import com.ysg.utils.YStringUtil;

public class HomeRankAdapter extends BaseQuickAdapter<HomeRankEntity, BaseDataBindingHolder<ItemHomeRankBinding>> {

    private RankEnum rankEnum;
    private HomeRankDataVM viewModel;
    private Presenter mPresenter = new Presenter();


    public HomeRankAdapter(HomeRankDataVM viewModel, RankEnum rankEnum) {
        super(R.layout.item_home_rank);
        this.viewModel = viewModel;
        this.rankEnum = rankEnum;
    }

    @Override
    protected void convert(BaseDataBindingHolder<ItemHomeRankBinding> holder, HomeRankEntity item) {
        ItemHomeRankBinding binding = holder.getDataBinding();
        if (binding != null) {
            int position = holder.getBindingAdapterPosition();
            binding.setEntity(item);
            binding.setViewModel(viewModel);
            binding.setPresenter(mPresenter);
            binding.setPosition(position);
            binding.executePendingBindings();

            //数字
            if (position > 2) {
                binding.tvNum.setText((1 + position) + "");
                binding.tvNum.setBackgroundResource(R.drawable.shape_rank_item_num);
            } else {
                binding.tvNum.setText("");
                if (0 == position){
                    binding.tvNum.setBackgroundResource(R.mipmap.ic_rank_one);
                }else if (1 == position){
                    binding.tvNum.setBackgroundResource(R.mipmap.ic_rank_two);
                }else if (2 == position){
                    binding.tvNum.setBackgroundResource(R.mipmap.ic_rank_three);
                }
            }
            //头像
            YImageUtil.show(binding.ivAvatar, item.getPic());
            //性别
            if (YStringUtil.eq(2, item.getSex())){
                binding.ivSex.setImageResource(R.mipmap.ic_girl);
            }else{
                binding.ivSex.setImageResource(R.mipmap.ic_boy);
            }
            //
             binding.tvAge.setText(item.getYearOld() + getContext().getResources().getString(R.string.year_old));
            //
            if (RankEnum.charm == rankEnum){
                binding.tvTotal.setText(rankEnum.getValue() + item.getTotalMeiLi());
            }else{
                binding.tvTotal.setText(rankEnum.getValue() + item.getTotalMoney());
            }
            //个人签名
            if (YStringUtil.isNotEmpty(item.getMySign())){
                binding.tvSignature.setText(item.getMySign());
            }

            if (RankEnum.love == rankEnum){
                binding.viewOther.setVisibility(View.GONE);
                binding.viewLove.setVisibility(View.VISIBLE);
                setNumUI(binding.tvNumLove, position);
                YImageUtil.show(binding.ivAvatarBoy, item.getPicA());
                YImageUtil.show(binding.ivAvatarGirl, item.getPicB());
                binding.tvNameBoy.setText(item.getNickA());
                binding.tvNameGirl.setText(item.getNickB());
                binding.tvQmd.setText(getContext().getResources().getString(R.string.intimacy)+ ":" + item.getTotalIntimacy() + "℃");
            }else{
                binding.viewOther.setVisibility(View.VISIBLE);
                binding.viewLove.setVisibility(View.GONE);
            }
        }
    }

    public static class Presenter{
        public void onItemClick(HomeVM viewModel, HomeResult entity){
            viewModel.jumpPersonDetail(entity);
        }
        public void onHeartClick(HomeVM viewModel, HomeResult entity, int position){
            viewModel.heart(entity, position);
        }
        public void onChatClick(HomeVM viewModel, HomeResult entity){
            viewModel.jumpChat(entity);
        }
    }

    private void setNumUI(TextView tvNum, int position){
        if (position > 2) {
            tvNum.setText((1 + position) + "");
            tvNum.setBackgroundResource(R.drawable.shape_rank_item_num);
        } else {
            tvNum.setText("");
            if (0 == position){
                tvNum.setBackgroundResource(R.mipmap.ic_rank_one);
            }else if (1 == position){
                tvNum.setBackgroundResource(R.mipmap.ic_rank_two);
            }else if (2 == position){
                tvNum.setBackgroundResource(R.mipmap.ic_rank_three);
            }
        }
    }







}
